<?php
/**
 *  本类不负责给键加前缀 Memcache 支持
 *  配置要求
 *  $GLOBALS['Dn_MemServers']=array('host'=>$host,'port'=>'port');
 *  $GLOBALS['Dn_MemServers']=array(0=>array('host'=>$host,'port'=>'port'));
 */
class DnCacheMemCache extends DnCache//implements IDnCache
{
	public $cache = null;
	public $expire = 1200; // 20*60
	public $is_connect = false;
	public $prefix = '';
	public $config=array(
		'expire'=>1200,
		'prefix'=>'',
		'servers'=>array(array('host'=>'','port'=>'')),
	);
	
	public function connect() {
		$this->cache = new Memcached();
		$flag = false;
		$servers=$this->config['servers'];
		foreach ($servers as $config) {
			$flag = $flag || $this->cache->addServer($config['host'], $config['port']);
		}
		
		$this->is_connect = $flag;
		return $falg;
	}
	///////////////////////////////////////////////////////////////////////////
	public function _get($key) {
		if (!$this->cache) {
			$this->connect();
		}
		//TODO 报错
		return $this->cache->get($this->config['prefix'].$key);
	}
	public function _set($key, $value, $expire = 0) {
		if (!$this->cache) {
			$this->connect();
		}
		if (!$expire){
			$expire = $this->config['expire'];
		}
		return $this->cache->set($this->config['prefix'].$key, $value, $expire);
	}
	public function _delete($key) {
		if (!$this->cache) {
			$this->connect();
		}
		return $this->cache->delete($this->config['prefix'].$key);
	}
}